package com.zhou.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.util.NumberUtil;
import com.zhou.util.ObjectUtil;
import com.zhou.util.StringTool;
import com.zhou.framework.util.MybatisPlusBatchUtil;
import com.zhou.message.dao.MessageTemplate;
import com.zhou.message.dao.MessageTemplateReceiver;
import com.zhou.message.dto.MessageTemplateQueryDto;
import com.zhou.message.mapper.MessageTemplateMapper;
import com.zhou.message.service.MessageTemplateReceiverService;
import com.zhou.message.service.MessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lang.zhou
 * @since 2023-03-01
 */
@Service
public class MessageTemplateServiceImpl extends ServiceImpl<MessageTemplateMapper, MessageTemplate> implements MessageTemplateService {

    @Autowired
    private MessageTemplateReceiverService templateReceiverService;

    @Override
    public MessageTemplate getTemplateByCode(String code) {
        LambdaQueryWrapper<MessageTemplate> w = new LambdaQueryWrapper<>();
        w.eq(MessageTemplate::getCode, code);
        return baseMapper.selectOne(w);
    }

    @Override
    public List<MessageTemplate> getTemplateByProcessType(int processType) {
        LambdaQueryWrapper<MessageTemplate> w = new LambdaQueryWrapper<>();
        w.eq(MessageTemplate::getProcessType, processType);
        return list(w);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMessageTemplate(MessageTemplate template,List<MessageTemplateReceiver> receivers) {
        if(!NumberUtil.upper0(template.getId())){
            save(template);
        }else{
            updateById(template);
            MybatisPlusBatchUtil.delete(templateReceiverService,MessageTemplateReceiver::getTemplateId,template.getId());
        }
        if(ObjectUtil.isNotEmpty(receivers)){
            for (MessageTemplateReceiver receiver : receivers) {
                receiver.setTemplateId(template.getId());
            }
            templateReceiverService.saveBatch(receivers);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMessageTemplate(List<Integer> templateIdList) {
        if(ObjectUtil.isNotEmpty(templateIdList)){
            removeByIds(templateIdList);
            MybatisPlusBatchUtil.batchDelete(templateReceiverService,MessageTemplateReceiver::getTemplateId,templateIdList);
        }
    }
    @Override
    public List<String> getAllMessageTemplateCatalog() {
        List<MessageTemplate> list = list();
        return list.stream().filter(o-> StringTool.isNotBlank(o.getCatalog())).map(MessageTemplate::getCatalog).distinct().collect(Collectors.toList());
    }

    @Override
    public List<MessageTemplate> getMessageTemplateList(MessageTemplateQueryDto dto) {
        return baseMapper.getMessageTemplateList(dto);
    }
}
